'''
Created on Mar 7, 2016

@author: JimmyMo
'''
import pymongo
from common import commonutil
from writter.perfsynwritter import Writter

class Writter4MongoDb(Writter):
    def _parse_options(self):
        self.host = "localhost"
        self.port = 27017
    def flush_db(self):
        raise NotImplementedError()
    
    def _write(self):
        
        devices = {}
        devices_l = []
        
#         for log in self.log4all_devices:
        for log in self.event_buffer_list:
            device_key = log["devicename"]
            port_key = commonutil.get_port_name(log, 'portname', commonutil.DEFAULT_PORTNAME)
#             port_key = "_default"
#             if "portname" in log.keys():
#                 port_key = log["portname"]
                
            if not device_key in devices.keys():
                devices[device_key] = {}
            
            devices[device_key][port_key] = log
        devices_l = list(devices.values())
        
        client = pymongo.MongoClient(self.host, self.port)
        db = client.logdb
        collection = db.logcollection
        print('inserting mongo...')
        collection.insert_many(devices_l)
        print('done...')
        pass